package com.venusource.wechat.web;

import com.venusource.exception.BusinessException;
import com.venusource.wechat.service.IWechatService;
import com.venusource.wechat.vo.AgentConfig;
import com.venusource.wechat.vo.Config;
import com.venusource.wechat.vo.TextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/wechat")
public class WechatCtrl {

  @Autowired
  private IWechatService wechatService;

  @GetMapping("/redirect")
  public String redirect(@RequestParam String url, @RequestParam(defaultValue = "STATE") String state) {
    return wechatService.redirect(url, state);
  }

  @GetMapping("/authorize")
  public String authorize(String code) throws BusinessException {
    return wechatService.authorize(code);
  }


  @GetMapping("/config")
  public Config getConfig(String url) {
    return wechatService.getConfig(url);
  }

  @GetMapping("/config/agent")
  public AgentConfig getAgentConfig(String url) {
    return wechatService.getAgentConfig(url);
  }

  @PostMapping("/send")
  public void sendMessage(@RequestBody TextMessage textMessage){
    wechatService.send(textMessage);
  }
}
